<?php

namespace App\Http\Controllers\Backstage\Article;

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\URL;
use Illuminate\Http\Request;

//文章标签管理

class ArticleTagController extends Controller
{
    //获取标签列表
    public function getTagList(Request $request)
    {
        $token = $request->query('token');
        //验证权限
        $verifytoken = verifyBackstageToken($token);
        if (is_array($verifytoken)) {
            return $verifytoken;
        }
        // $info = DB::table('article')->get();
        $page = (int)$request->query('page');
        $pageSize = (int)$request->query('pageSize');
        //翻页
        $page = $page - 1;
        if ($page != 0) {
            $page = $pageSize * $page;
            $pageSize = $pageSize * $page;
        }
        //联表查询
        $count = DB::table('article_tag')->count();
        $info = DB::table('article_tag')
            ->offset($page)
            ->limit($pageSize)
            ->get();
        if ($info) {
            $info=json_decode($info,true);
            foreach ($info as $key => $value) {
                $info[$key]['create_time']=date("Y-m-d",$value['create_time']);
            }
            $data = success();
            $data['data'] = $info;
        } else {
            $data = fail();
        }
        $data['count'] = $count;
        return $data;
    }
    //新增标签
    public function addTag(Request $request){
        $token = $request->input('token');
        //验证权限
        $verifytoken = verifyBackstageToken($token);
        if (is_array($verifytoken)) {
            return $verifytoken;
        }
        $date['tag_name']=addslashes($request->input('name'));
        $date['create_time']=time();
        $msg=DB::table('article_tag')->insert($date);
        if($msg){
            $data=success();
        }else{
            $data=fail();
        }
        return $data;
    }
    //修改标签
    public function updateTag(Request $request){
        $token = $request->input('token');
        //验证权限
        $verifytoken = verifyBackstageToken($token);
        if (is_array($verifytoken)) {
            return $verifytoken;
        }
        $map['tag_id']=(int)$request->input('tag_id');
        $date['tag_name']=addslashes($request->input('name'));
        $date['create_time']=time();
        $msg=DB::table('article_tag')->where($map)->update($date);
        if($msg){
            $data=success();
        }else{
            $data=fail();
        }
        return $data;
    }

    //删除标签
    public function delTag(Request $request){
        $token = $request->query('token');
        //验证权限
        $verifytoken = verifyBackstageToken($token);
        if (is_array($verifytoken)) {
            return $verifytoken;
        }
        $map['tag_id']=(int)$request->query('tag_id');
        $msg=DB::table('article_tag')->where($map)->delete();
        if($msg){
            $data=success();
        }else{
            $data=fail();
        }
        return $data;
    }
}
